
/**
 * hook 导出函数_Z13MD5_CalculatePKcjPc验证参数
 * @type {NativePointer}
 */
const MD5_Calculate = Module.findExportByName("librelease_sig.so", "_Z13MD5_CalculatePKcjPc")
if (MD5_Calculate !== null) {
    Interceptor.attach(MD5_Calculate, {
        onEnter: function (args) {
            const byteArray = args[0].readByteArray(args[1].toInt32());
            const hexdata = hexdump(byteArray, {
                ansi: true,
                length: args[1].toInt32(),
            });
            console.log(hexdata);
            this.retbuff = args[2]
        },
        onLeave: function (args) {
            const ret = this.retbuff.readCString();
            console.log("result", ret);
        }
    });
}


const BUF_SIZE = 16;

class StdString {
    constructor(addr) {
        this.addr = addr;
    }

    get bufAddr() {
        if(this.reservedSize.compare(16) > 0) {
            return this.addr.readPointer();
        } else {
            return this.addr;
        }
    }

    get size() {
        return this.addr.add(BUF_SIZE).readPointer();
    }

    get reservedSize() {
        return this.addr.add(BUF_SIZE).add(Process.pointerSize).readPointer();
    }

    toString() {
        const size = this.size;
        if(size.isNull()) {
            return "<EMPTY std::string>";
        }
        return Memory.readCString(this.bufAddr, size.toInt32());
    }
}

class StdVector {
    constructor(addr, options) {
        this.addr = addr;
        this.elementSize = options.elementSize ? options.elementSize : Process.pointerSize;
        this.introspectElement = options.introspectElement;
    }

    get myfirst() {
        return this.addr.readPointer();
    }

    get mylast() {
        return this.addr.add(Process.pointerSize).readPointer();
    }

    get myend() {
        return this.addr.add(2 * Process.pointerSize).readPointer();
    }

    countBetween(begin, end) {
        if(begin.isNull()) {
            return 0;
        }
        const delta = end.sub(begin);
        return delta.toInt32() / this.elementSize;
    }

    get size() {
        return this.countBetween(this.myfirst, this.mylast);
    }

    get capacity() {
        return this.countBetween(this.myfirst, this.myend);
    }

    toString() {
        let r = "std::vector(" + this.myfirst + ", " + this.mylast + ", " + this.myend + ")";
        r += "{ size: " + this.size + ", capacity: " + this.capacity;
        if(this.introspectElement) {
            r += ", content: [";
            const first = this.myfirst
            if(!first.isNull()) {
                const last = this.mylast;
                for(let p = first; p.compare(last) < 0; p = p.add(this.elementSize)) {
                    if(p.compare(first) > 0) {
                        r += ", ";
                    }
                    r += this.introspectElement(p);
                }
            }
            r += "]";
        }
        r += " }";
        return r;
    }
}


// GeneratorSIG(
//     std::string &,
//     std::vector<std::string> &,
//     std::string &,
//     std::string &,
//     std::string &,
//     std::string &,
//     std::string &
// );

const GeneratorSIG = Module.findExportByName("librelease_sig.so", "_Z12GeneratorSIGRSsRSt6vectorISsSaISsEES_S_S_S_S_")
console.log("GeneratorSIG", GeneratorSIG);
if (GeneratorSIG !== null) {
    Interceptor.attach(GeneratorSIG, {
        onEnter: function (args) {
            console.log("args 0", new StdString(args[0]));
            console.log("args 1", new StdVector(args[1], {}));
            console.log("args 2", new StdString(args[2]));
            console.log("args 3", new StdString(args[3]));
            console.log("args 4", new StdString(args[4]));
            console.log("args 5", new StdString(args[5]));
            console.log("args 6", new StdString(args[6]));
        },
        onLeave: function (args) {
        }
    });
}
